<template>
  <div>
    <el-table
      :data="data"
      border
       height="300"
      style="width: 100%"
      :summary-method="getSummaries"
    show-summary
    >
      <el-table-column
        prop="rating"
        label="评分"
        width="160"
      >
        <template slot-scope="scope">
          <el-rate
            v-model=" scope.row.rating "
            disabled
            show-score
            text-color="#ff9900"
            score-template="{value}"
          >
          </el-rate>

        </template>
      </el-table-column>
      <el-table-column
        prop="comment"
        label="内容"
        width="160"
      >
      </el-table-column>
      <el-table-column
        prop="createdAt"
        width="120"
        label="日期"
      >
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { listReviews } from '@/api/system/reviews'
export default {
  props: {
    value: Number,
  },
  data() {
    return {
      data: [],
    }
  },
  watch: {
    value: {
      handler(val) {
        if (val) {
          this.getData()
        }
      },
      deep: true,
      immediate: true,
    },
  },
  methods: {
    getSummaries(param) {
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          if (index === 0) {
            let data = 0
            this.data.forEach(v=>{
                data+= v.rating
            })
            sums[index] = data ? (data / this.data.length).toFixed(1): 0;
            return;
          }else{
            sums[index] = '';
          }
        });

        return sums;
      },
    getData() {
      listReviews({ userId: this.value }).then((v) => {
        this.data = v.rows
      })
    },
  },
}
</script>

<style>
</style>